package cn.ffcs.uec.view.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uec.view.dao.UecInnovationDao;
import cn.ffcs.uec.view.model.UecInnovation;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecInnovationDao")
@Transactional
public class UecInnovationDaoImpl extends BaseDaoImpl implements UecInnovationDao {

	@Override
	public PageInfo queryPageInfoByUecInnovation(UecInnovation uecInnovation,
			int current, int pageSize) {
		if (null != uecInnovation) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_INNOVATION T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (!StrUtil.isNullOrEmpty(uecInnovation.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecInnovation.getPkPsndoc());
			}
			//sb.append(" AND to_number(to_char(T.gettime,'yyyy')) >= (Extract(year from sysdate)-3)");
			sb.append(" order by T.gettime ");	
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecInnovation.class);
		}
		return null;
	}

	@Override
	public UecInnovation findUecInnovationById(UecInnovation uecInnovation) {
		if (null != uecInnovation) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_INNOVATION T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecInnovation.getInnovationId())) {
				sb.append(" AND T.innovation_id = ? ");
				params.add(uecInnovation.getInnovationId());
			}
			//sb.append(" AND to_number(to_char(T.gettime,'yyyy')) >= (Extract(year from sysdate)-3)");
			sb.append(" order by T.gettime ");	
			List<UecInnovation> uecEncs = this.jdbcFindList(sb.toString(), params,
					UecInnovation.class);
			if (uecEncs.size() > 0) {
				return uecEncs.get(0);
			} 
		}
		return null;
	}

/**
 * 近三年情况
 */
	@Override
	public List<UecInnovation> findUecInnovationByPkPsndocAndDiffDate(StaffAndOutStaff staffAndOutStaff) {
		if (null != staffAndOutStaff) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_INNOVATION T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(staffAndOutStaff.getPkPsndoc());
				sb.append(" AND to_number(to_char(T.gettime,'yyyy')) >= (Extract(year from sysdate)-3)");
				sb.append(" order by T.gettime");		
			return this.jdbcFindList(sb.toString(), params, UecInnovation.class);
		}
		return null;
	}

	@Override
	public List<UecInnovation> findUecInnovationByPkPsndoc(StaffAndOutStaff staffAndOutStaff) {
		if (null != staffAndOutStaff) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_INNOVATION T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if(!StrUtil.isNullOrEmpty(staffAndOutStaff.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(staffAndOutStaff.getPkPsndoc());
				//sb.append(" AND to_number(to_char(T.gettime,'yyyy')) >= (Extract(year from sysdate)-3)");
				sb.append(" order by T.gettime ");	
			}
			System.out.println("sb----------------"+sb.toString());
			return this.jdbcFindList(sb.toString(), params, UecInnovation.class);
		}
		return null;
	}

}
